<?php
/**
 * +----------------------------------------------------------------------
 * | Author: 罗极
 * +----------------------------------------------------------------------
 * | Description: Log.php
 * +----------------------------------------------------------------------
 * | User: Administrator Date: 8/26/ 0026 Time: 16:14
 * +----------------------------------------------------------------------
 */
namespace Framework\Log;

use Framework\Log\Abstracts\LogAbstract;
use Framework\Log\Exception\CanNotCreateException;
use Framework\Log\Exception\CanNotWriteException;
use Framework\Log\Exception\NotFoundException;

class Log extends LogAbstract
{
    public static function write(string $msg, string $path, string $filename = '')
    {
        $path = $path.DIRECTORY_SEPARATOR;

        if(empty($filename)) {
            $filename = self::defaultLogName();
        }

        if(!file_exists($path)) {
            if(!mkdir($path, 0666, true)) {
                throw new CanNotCreateException(sprintf(
                    '日志目录不能被创建: [%s]',
                    $path
                ));
            }
        }


        if(!file_exists($path.$filename)) {
            if(!touch($path.$filename)) {
                throw new CanNotCreateException(sprintf(
                    '日志文件不能被创建: [%s]',
                    $path.$filename
                ));
            }
        }

        $str = self::defaultStyle($msg);

        if(!file_put_contents($path.$filename, $str, FILE_APPEND)) {
            throw new CanNotWriteException(sprintf(
                '日志文件不能写入: [%s]',
                $path.$filename
            ));
        }

    }
}